//
//  LXPlanCollectionView.m
//  ZuLinProject
//
//  Created by bancheng on 2022/9/15.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXPlanCollectionView.h"
#import "LXSignContractModel.h"
@interface LXPlanCollectionView()

@property (nonatomic)LXSignContractModel *selModel;

@end
@implementation LXPlanCollectionView


-(void)awakeFromNib{
    [super awakeFromNib];
    [self.backView addCorner:5];
    
    
    self.selModel = [LXSignContractModel new];
    
    [self.cancelBtn addCorner:3 AndLine:ColorWith3C6CFC];
    [self.confirmBtn addCorner:3];
    self.amountTextField.delegate = self;
    self.remarkTextField.delegate = self;
    [self.closeBtn whenTapped:^{
        [self removeFromSuperview];
    }];
    [self.cancelBtn whenTapped:^{
        [self removeFromSuperview];
    }];
    
    
    // 收款计划
//    [self.collectionTypeView whenTapped:^{
//        [self endEditing:YES];
//        NSArray *ary = @[@"租金",@"押金",@"首付款",@"月供",@"尾款"];
//        SinglePickerView *singPickView = [[SinglePickerView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, ScreenHeight)];
//        for (int i = 0; i<ary.count; i++) {
//            if ([self.collectionTypeLab.text isEqualToString:ary[i]]) {
//                singPickView.selectInteger = i;
//            }
//        }
//        [singPickView pickViewWithArray:ary andDescArray:nil andTitleStr:@"请选择收款计划项"];
//        [[UIApplication sharedApplication].delegate.window addSubview:singPickView];
//        [singPickView showBottomView];
//        singPickView.block = ^(NSString *singleTitle){
//            self.collectionTypeLab.text = singleTitle;
//            if (self.type == 1) {
//                self.selModel.fundType = [NSString getCollectionTypeNameWithName:singleTitle];
//            }
//            else {
//                self.model.fundType = [NSString getCollectionTypeNameWithName:singleTitle];
//
//            }
//
//
//        };
//    }];
    
    // 收款日期
    [self.collectionDateView whenTapped:^{
        [self endEditing:YES];
        DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, ScreenHeight) withDateStr:self.collectionDateLab.text withIsMaxDate:YES withNoForward:NO WithTips:@"请选择应收日期"] ;
        [datePicker showBoomView];
        [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
        
        datePicker.block = ^(NSString *yearString){
            self.collectionDateLab.text = yearString;
//            if (self.type == 1) {
//                self.selModel.exceptReceiveDate = yearString;
//            }
//            else {
//                self.model.exceptReceiveDate = yearString;
//            }
           
        };
        
        
    }];
    
}
- (void)setModel:(LXSignContractModel *)model{
    _model = model;
    
    if (self.type == 2) {
        self.collectionTypeLab.text = [NSString getCollectionNameWithType:model.fundType];
        self.collectionDateLab.text = model.exceptReceiveDate;
        self.amountTextField.text = model.amount;
        self.remarkTextField.text = model.remark;
        
    }
    else {
        
        self.collectionTypeLab.text = [NSString getCollectionNameWithType:model.fundType];
        self.collectionDateLab.text = [NSString getNowDateWithFormat:@"yyyy-MM-dd"];
        self.selModel.exceptReceiveDate = [NSString getNowDateWithFormat:@"yyyy-MM-dd"];
        self.selModel.fundType = self.model.fundType;
    }
    
}




- (void)textFieldDidEndEditing:(UITextField *)textField{
//    if (textField == self.amountTextField) {
//        if (self.type == 1) {
//            self.selModel.amount = textField.text;
//        }
//        else {
//            self.model.amount = textField.text;
//        }
//    }
//    else {
//        if (self.type == 1) {
//            self.selModel.remark = textField.text;
//        }
//        else {
//            self.model.remark = textField.text;
//        }
//    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *shouldText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (textField == self.amountTextField) {
        NSInteger maxLength = 8;
        NSInteger strLength = shouldText.length;
        
        if (strLength > maxLength) {
            // 允许删除
            if (strLength < textField.text.length) {
                return YES;
            }
            
            return NO;
        }
        
        
        return [NSString validateNumberNegativePoint:shouldText];
     
    }
    
    
    return YES;
    
}

- (IBAction)confirmAction:(UIButton *)sender {
    
    
    if (StringIsEmpty(self.amountTextField.text)) {
        showAlert(@"请输入应收金额");
        return;
    }
    
    
    if (self.type == 1) {
        self.selModel.amount = self.amountTextField.text;
    }
    else {
        self.model.amount = self.amountTextField.text;
    }
    
    
    if (self.type == 1) {
        self.selModel.remark = self.remarkTextField.text;
    }
    else {
        self.model.remark = self.remarkTextField.text;
    }
    
    if (self.type == 1) {
        self.selModel.exceptReceiveDate = self.collectionDateLab.text;
    }
    else {
        self.model.exceptReceiveDate = self.collectionDateLab.text;
    }
    
    
    if (self.clickPlanAction) {
        
        if (self.type == 1) {
            self.clickPlanAction(self.selModel);
        }
        else {
            self.clickPlanAction(self.model);
        }
    }

    [self removeFromSuperview];
   
}

@end
